package miblab.school;


import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
import miblab.school.SystemParameters;



public class SetLoc extends Activity{

	private static  final String TAG = SetLoc.class.getSimpleName();
	public Location location;
	public double homelat;
	public double homelon;
	public long hometime;
	public double schlat;
	public double schlon;
	public long schtime;
	public double hosplat;
	public double hosplon;
	public long hosptime;
	public LocationManager lm;
	public LocationListener ll;
	public boolean gps_enabled = true;
	boolean status = false;

	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.scr3a);

		//**********************Initialize Location Variables*******************************		

		final LocationManager lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
		final LocationListener ll = new mylocationlistener();
		lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, ll);
		try{gps_enabled=lm.isProviderEnabled(LocationManager.GPS_PROVIDER);}catch(Exception ex){}
		

		Button submit = (Button) findViewById(R.id.button4);
		Button home = (Button) findViewById(R.id.button1);
		Button school = (Button) findViewById(R.id.button2);
		Button hospital = (Button) findViewById(R.id.button3);

		//**********************Home Button Listener ****************************************		
		home.setOnClickListener(new View.OnClickListener() {
			public void onClick(View view) {
				set(0);
			}
		});

		//**********************School Button Listener ****************************************			
		school.setOnClickListener(new View.OnClickListener() {
			public void onClick(View view) {

				set(1);
			}
		});

		//**********************School Button Listener ****************************************	
		hospital.setOnClickListener(new View.OnClickListener() {
			public void onClick(View view) {

				set(2);
			}
		});  

		//**********************Submit Button Listener ****************************************			
		submit.setOnClickListener(new View.OnClickListener() {
			public void onClick(View view) {
				Intent intent = new Intent(SetLoc.this,ScreenSchoolActivity.class);
				startActivity(intent);
				Toast.makeText(SetLoc.this, "Changes Submitted", Toast.LENGTH_LONG).show();

			}
		});
	}

	//**********************Location Setting***********************************************
	public void set(int locID)
	{


		try {
			if(gps_enabled)
			{

				//Setting first location
				lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, ll);

				//storing it in values
				location = lm.getLastKnownLocation(LocationManager.GPS_PROVIDER);

				if(location!=null)
				{
					Log.i(TAG,"LOCATION CHANGED"+location.getLatitude()+"");
					Log.i(TAG,"LOCATION CHANGED"+location.getLongitude() +"");
					Log.i(TAG,"LOCATION CHANGED"+location.getTime() + "");

					switch(locID)
					{

					case 0:
					{
						homelat = location.getLatitude();
						homelon = location.getLongitude();
						hometime = location.getTime();
						status = SDwrite("Home.txt",homelat,homelon,hometime);
						break;
					}
					case 1:
					{
						schlat = location.getLatitude();
						schlon = location.getLongitude();
						schtime = location.getTime();
						status = SDwrite("School.txt",schlat,schlon,schtime);
						break;
					}
					case 2:
					{
						hosplat = location.getLatitude();
						hosplon = location.getLongitude();
						hosptime = location.getTime();
						status = SDwrite("Hospital.txt",hosplat,hosplon,hosptime);
						break;
					}

					}

					if(status)
						Toast.makeText(SetLoc.this, SystemParameters.LocName[locID]+"Location changed", Toast.LENGTH_LONG).show();
					else
					{
						Toast.makeText(SetLoc.this, "Check Internet", Toast.LENGTH_LONG).show();
					}

				}

			}	
		} catch (NullPointerException e1) { 
			Log.i(TAG,"Error 1");
			e1.printStackTrace();
		}
	}
	//**********************On Pause ******************************************************		
	@Override
	protected void onPause() {
		// TODO Auto-generated method stub
		super.onPause();
		finish();
	}
	@Override
	public void onBackPressed() {
		//Log.d("CDA", "onBackPressed Called");
		Intent intent = new Intent(SetLoc.this,ScreenSchoolActivity.class);
		startActivity(intent);
	}
	//**********************SD Write ******************************************************		
	public boolean SDwrite(String sFileName, double lat, double lon, double time)
	{
		boolean state = false;
		try
		{
			File root = new File(SystemParameters.path, "School");
			if (!root.exists()) {
				root.mkdirs();
			}
			File gpxfile = new File(root,sFileName);
			FileWriter writer = new FileWriter(gpxfile);
			writer.write("Lat:"+lat+":Lon:"+lon+":Tim:"+time);
			writer.flush();
			writer.close();
			state = true;
		}
		catch(IOException e)
		{
			Toast.makeText(this, "Insert SD Card", Toast.LENGTH_SHORT).show();
			e.printStackTrace();
			state = false;
		}

		return state;

	}

	//**********************Location listener ************************************************		
	class mylocationlistener implements LocationListener {
		@Override
		public void onLocationChanged(Location location) 
		{

		}
		@Override
		public void onProviderDisabled(String provider) {
		}
		@Override
		public void onProviderEnabled(String provider) {
		}
		@Override
		public void onStatusChanged(String provider, int status, Bundle extras) {
		}
	};

}